package client.calendar;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.GroupLayout;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.Alignment;

import miscellaneous.Storage;
import mockup.AppointmentMockup;
import mockup.MeetingMockup;
import mockup.Mockup;
import mockup.MockupFactory;
import mockup.MockupToData;
import mockup.UserMockup;

import data.Appointment;
import data.Meeting;
import data.PropertyChangeSpeaker;
import data.User;

public class CalendarSearchPane extends JFrame implements PropertyChangeSpeaker{
	private JTextField searchField;
	private JButton myCalendarButton;
	private JLabel searchLabel;
	private JList searchList;
	private JButton showCalendarButton;
	private JPanel pane;
	
	private DefaultListModel employeesModel;
	private ArrayList<User> employeeList;
	
	private PropertyChangeSupport pcs;
	
	public CalendarSearchPane() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("resources/icons/aCalendarIcon.png"));
		pcs = new PropertyChangeSupport(this);
		employeeList = Storage.users.getCopyOfList();
		employeesModel = new DefaultListModel();
		//Add users to the defaultListModel
		for (User user : employeeList) {
			employeesModel.addElement(user);
		}
		
		setTitle("Velg bruker");
		pane = new JPanel();
		pane.setPreferredSize(new Dimension(202, 450));
		
		searchLabel = new JLabel("Søk:");
		searchField = new JTextField();
			searchField.addKeyListener(new SearchKeyListener());
			searchField.setColumns(10);
		
		myCalendarButton = new JButton("Min kalender");
			myCalendarButton.addActionListener(new ButtonActionListener());
		showCalendarButton = new JButton("Vis kalender");
			showCalendarButton.addActionListener(new ButtonActionListener());
		
		JScrollPane scrollPane = new JScrollPane();
		searchList = new JList(employeesModel);
		searchList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		scrollPane.setViewportView(searchList);
		
		getContentPane().add(pane);
		GroupLayout gl_pane = new GroupLayout(pane);
		gl_pane.setHorizontalGroup(
			gl_pane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(10)
					.addComponent(myCalendarButton, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_pane.createParallelGroup(Alignment.LEADING)
						.addComponent(searchLabel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_pane.createSequentialGroup()
							.addGap(29)
							.addComponent(searchField, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE))))
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(10)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(53)
					.addComponent(showCalendarButton))
		);
		gl_pane.setVerticalGroup(
			gl_pane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(13)
					.addComponent(myCalendarButton)
					.addGap(11)
					.addGroup(gl_pane.createParallelGroup(Alignment.LEADING)
						.addComponent(searchLabel, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 317, GroupLayout.PREFERRED_SIZE)
					.addGap(7)
					.addComponent(showCalendarButton))
		);
		pane.setLayout(gl_pane);

		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	public void addPropertyChangeListener(PropertyChangeListener listener){
		pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener){
		pcs.removePropertyChangeListener(listener);
	}

	public class ButtonActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == myCalendarButton){
				pcs.firePropertyChange("userChange", null, Storage.getloggedInUser());
			}else if(e.getSource() == showCalendarButton){
				User u = (User) searchList.getSelectedValue();
				
				UserMockup askforApp = MockupFactory.getUserMockup(u);
				askforApp.setSessionId(Storage.getSessionID());
				askforApp.setState(Mockup.GET_APPOINTMENTS);
				
				@SuppressWarnings("unchecked")
				ArrayList<AppointmentMockup> list = (ArrayList<AppointmentMockup>)
					(Storage.getSocket().send(askforApp));
				for(AppointmentMockup m : list ) {
					Storage.appointments.insert((Appointment)MockupToData.getData(m));	
				}
				
				UserMockup askforMeeting = MockupFactory.getUserMockup(u);
				askforMeeting.setSessionId(Storage.getSessionID());
				askforMeeting.setState(Mockup.GET_MEETINGS);
				
				@SuppressWarnings("unchecked")
				ArrayList<MeetingMockup> list2 = (ArrayList<MeetingMockup>)
					(Storage.getSocket().send(askforMeeting));
				for(MeetingMockup m : list2 ) {
					Storage.meetings.insert((Meeting)MockupToData.getData(m));	
				}
				
				pcs.firePropertyChange("userChange", null, u);
			}
			dispose();
		}
		
	}
	
	public class SearchKeyListener implements KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
		}

		@Override
		public void keyReleased(KeyEvent e) {
			employeesModel.clear();
			String searchtText = searchField.getText();
			int searchLength = searchtText.length();
			
			if(searchtText.equals("")){
				for (User user : employeeList) {
					employeesModel.addElement(user);
				}
			}else {
				for (User user : employeeList) {
					int userNameLength = user.getName().length();
					if(userNameLength < searchLength) {
						continue;
					}else if(user.getName().substring(0, searchLength).equalsIgnoreCase(searchtText)){
					employeesModel.addElement(user);
					
					}
				}
			}
			searchList.validate();
			searchList.updateUI();
		}

		@Override
		public void keyTyped(KeyEvent e) {
		}
		
	}
}
